load_role_configuration_list = {
    load_role_columns : function(){
	var columns = [
		{"mData": function(source, type, val) {
			return "";
		    }
		},
		{ "mData": "role_name" },
		{"mData": function(source, type, val) {
		    $actions = "";
		    $actions += '<div class="TAC" data-id="' + source['data_reference_id'] + '">';
		    $actions += '<a data-action="role" data-type="edit" class="role_configuration_actions cursor"><span class="icon-pencil tip" data-original-title="Edit ' + source['role_name'] + '"></span></a>';
                    $actions += '<a data-action="role" data-type="remove" class="role_configuration_actions cursor"><span class="icon-trash tip" data-original-title="Remove ' + source['role_name'] + '"></span></a>';
		    $actions += '</div>';
		    return $actions;
		    }
		}
	    ];
	return columns;
    },
    load_role : function(){
	var others =  {start_sort: 1,sort_type: "desc",none_sortable: [0,2], allow_number : true, category_count : ""};
	table('#tbl_load_role',"/ajax/role-configuration?action=load_table_role&type=load",load_role_configuration_list.load_role_columns(),others).fnDestroy();
	table('#tbl_load_role',"/ajax/role-configuration?action=load_table_role&type=load",load_role_configuration_list.load_role_columns(),others);
    }
}
role_configuration = {
    set_role_configuration : function(){
	$("body").on("click",".role_configuration_actions",function(){
	    var data_action = $(this).attr("data-action");
	    var data_type = $(this).attr("data-type");
	    var dataID = $(this).parent().attr("data-id");
	    var data_json = $("body").data("get_query_" + dataID);
	    
	    switch (data_type) {
		case "edit":
		    switch (data_action) {
			case "role":
			    var action_url = '/ajax/role-configuration?action=update_' + data_action + '&type=update&dataID=' + data_json['roleID'];
			    $("#lbl_" + data_action).html("Update");
			    $("#add_" + data_action).attr('action',action_url);
			    $("#new_" + data_action).addClass("hide");
			    $("#update_" + data_action).removeClass("hide");
			    $('input[name="role_name"]').val(data_json['role_name']);
			    break;
		    }
		    break;
		
		case "remove":
		    switch (data_action) {
			case "role":
			    jConfirm( 'Remove Role', 'Are you sure do you want to delete this ' + data_action + '?','', '', '', function(e){
				if (e == true) {
				    $.post("/ajax/role-configuration?action=remove_role&type=remove&dataID=" + data_json['roleID'],{},function(data){
					var ret = JSON.parse(data);
					if (ret['notification'] == "Done.") {
					    showNotification({
						message: ret['msg'],
						type: "success",
						autoClose: true,
						duration: 3
					    });
					    load_role_configuration_list.load_role();
					}
				    })
				}
			    });
			    break;
		    }
	    }
	})
    },
    role_access_setup : function(){
	$("body").on("click","#access_add",function(){
	//    var options = { 
	//	dataType:      'json',
	//	beforeSubmit:  showRequest,
	//	success:       showResponse
	//    }; 
	    
	})
    }
}

tab_click = {
    trigger_role : function(){
	$("body").on("click","#get_load_role",function(){
	    load_role_configuration_list.role_access_setup();
	})
    }
}

$(document).ready(function(){
    var pathname = window.location.pathname
    if (pathname == "/settings/role-access") {
	load_role_configuration_list.load_role();
    }
    
    
    role_configuration.set_role_configuration();
    
    // Role Access Set Up
    //role_configuration.role_access_setup();
     
     
    var options = { 
	dataType:      'json',
	beforeSubmit:  showRequest,
	success:       showResponse
    }; 
    $('#add_role').validationEngine('attach');
    $('#add_role').ajaxForm(options);
    //console.log($('#add_role').validationEngine())
    
    $('#add_department_unit').validationEngine('attach');
    $('#add_department_unit').ajaxForm(options);
    
    $('#add_brand').validationEngine('attach');
    $('#add_brand').ajaxForm(options);
    
    $('#access_role').validationEngine('attach');
    $('#access_role').ajaxForm(options);
})


